package com.qidianit.mes.pro.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidianit.mes.pojo.Result;
import com.qidianit.mes.pojo.pro.WorkflowProcess;
import com.qidianit.mes.pro.mapper.ProcessMapper;
import com.qidianit.mes.pro.mapper.ProcessStepMapper;
import com.qidianit.mes.pro.mapper.WorkflowProcessMapper;
import com.qidianit.mes.pro.service.WorkflowProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Objects;

/**
 * @Description:
 * @Author: wang fei long
 * @Date: 2025/9/18 0018 - 19:29
 */
@Service
public class WorkflowProcessServiceImpl implements WorkflowProcessService {

    @Autowired
    private WorkflowProcessMapper workflowProcessMapper;
    @Autowired
    private ProcessMapper processMapper;
    @Autowired
    private ProcessStepMapper processStepMapper;

    @Override
    public Result<PageInfo<WorkflowProcess>> selectByWorkflowId(Long workflowId, int pageNUm, int pageSize) {
        PageHelper.startPage(pageNUm,pageSize);
        ArrayList<WorkflowProcess> workflowProcesses = workflowProcessMapper.selectByWorkflowId(workflowId);
        for (WorkflowProcess workflowProcess : workflowProcesses) {
            //将查询到的workflowProcess中的processId传入selectById方法中
            //将根据processId查询到的process封装到process对象中
            workflowProcess.setProcess(processMapper.selectById(workflowProcess.getProcessId()));
            //将查询到的workflowProcess中的processId 加1再 传入selectById方法中
            //将根据processId查询到的process封装到nextProcess对象中
            workflowProcess.setNextProcess(processMapper.selectById(workflowProcess.getProcessId()+1));
            workflowProcess.setRelationNextDict(workflowProcessMapper.selectDictById(workflowProcess.getRelationNext()));
        }
        return Result.success(new PageInfo<>(workflowProcesses));
    }

    @Override
    public Result insertWorkflowProcess(WorkflowProcess workflowProcess) {
        if (workflowProcess.getWorkflowId() == null ||
                workflowProcess.getProcessId() == null ||
                workflowProcess.getStepOrder() == null ||
                workflowProcess.getRelationNext() == null ||
                workflowProcess.getGanttColor() == null || "".equals(workflowProcess.getGanttColor()) ||
                workflowProcess.getIsCritical() == null ||
                workflowProcess.getNeedInspection() == null ||
                workflowProcess.getPrepTime() == null ||
                workflowProcess.getWaitTime() == null){
            return Result.error("添加失败，添加信息不完整");
        }
        ArrayList<WorkflowProcess> workflowProcesses = workflowProcessMapper.selectByWorkflowId(workflowProcess.getWorkflowId());
        for (WorkflowProcess process : workflowProcesses) {
            if(Objects.equals(process.getStepOrder(), workflowProcess.getStepOrder())){
                return Result.error("工序序号[" + process.getProcessId() + "]已经存在");
            }
        }
        return workflowProcessMapper.insertWorkflowProcess(workflowProcess)==1?Result.success():Result.error("操作失败");
    }


    @Override
    public Result<WorkflowProcess> selectById(Long id) {
        return Result.success(workflowProcessMapper.selectById(id));
    }


    @Override
    public Result updateById(WorkflowProcess workflowProcess) {
        if (workflowProcess.getWorkflowId() == null ||
                workflowProcess.getProcessId() == null ||
                workflowProcess.getStepOrder() == null ||
                workflowProcess.getRelationNext() == null ||
                workflowProcess.getGanttColor() == null ||"".equals(workflowProcess.getGanttColor()) ||
                workflowProcess.getIsCritical() == null ||
                workflowProcess.getNeedInspection() == null ||
                workflowProcess.getPrepTime() == null ||
                workflowProcess.getWaitTime() == null){
            return Result.error("修改失败，修改信息不完整");
        }
        ArrayList<WorkflowProcess> workflowProcesses = workflowProcessMapper.selectByWorkflowId(workflowProcess.getWorkflowId());
        for (WorkflowProcess process : workflowProcesses) {
            if(Objects.equals(process.getStepOrder(), workflowProcess.getStepOrder())
                    && !Objects.equals(process.getId(), workflowProcess.getId())){
                return Result.error("工序序号[" + process.getProcessId() + "]已经存在");
            }
        }
        return workflowProcessMapper.updateById(workflowProcess)==1?Result.success():Result.error("操作失败");
    }


    @Override
    public Result deleteById(Long id) {
        return workflowProcessMapper.deleteById(id)==1?Result.success():Result.error("操作失败");
    }

    @Override
    public Result batchDelete(Long[] ids) {
        return workflowProcessMapper.batchDelete(ids) > 0?Result.success():Result.error("操作失败");
    }


    @Override
    public Result<ArrayList<WorkflowProcess>> selectWorkflowProcessByWorkflowId(Long id) {
        ArrayList<WorkflowProcess> workflowProcesses = workflowProcessMapper.selectByWorkflowId(id);
        for (WorkflowProcess workflowProcess : workflowProcesses) {
            workflowProcess.setProcess(processMapper.selectById(workflowProcess.getProcessId()));
            workflowProcess.setNextProcess(processMapper.selectById(workflowProcess.getProcessId()+1));
            workflowProcess.setRelationNextDict(workflowProcessMapper.selectDictById(workflowProcess.getRelationNext()));
        }
        return null;
    }

}
